package com.hctforgreen.greenservice;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.taptwo.android.widget.MyXListView;
import org.taptwo.android.widget.MyXListView.IXListViewListener;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.hctforgreen.greenservice.DownloadManagerActivityV2.DownloadUpdateReceiver;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.examine.PaperActivity;
import com.hctforgreen.greenservice.model.ExamPaperEntity;
import com.hctforgreen.greenservice.model.StudyEntity;
import com.hctforgreen.greenservice.model.VideoTypeEntity;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity;
import com.hctforgreen.greenservice.ui.adapter.ExamPaperListAdapter;
import com.hctforgreen.greenservice.ui.adapter.StudyListAdapter;
import com.hctforgreen.greenservice.ui.adapter.VideoDetailAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
/**
 * 考试列表（搜索中）
 * @author Administrator
 *
 */
public class ExamListActivity extends ParentActivity implements
		IXListViewListener {

	private int pageNum = 1;// 当前页 初始值为1
	private MyXListView listView;
	private ExamPaperListAdapter mAdapter;
	private List<ExamPaperEntity> data = new ArrayList<ExamPaperEntity>();

	private String moduleId;
	private String childtypeId;
	private String searchKey;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study_list);
		initSkinLayout();
		initTitleButtonBar();
		initDataAndWindow();
		initFinshExamineActivityReceiver();
	}

	private void initDataAndWindow() {

		moduleId = getIntent().getStringExtra("moduleId");
		childtypeId = getIntent().getStringExtra("childtypeId");
		searchKey = getIntent().getStringExtra("searchKey");

		final View convertView = findViewById(R.id.lyt_default_content);
		listView = (MyXListView) convertView
				.findViewById(R.id.lst_default_list);
		listView.setPullRefreshEnable(false);
		listView.setPullLoadEnable(false);
		listView.setXListViewListener(this);
		mAdapter = new ExamPaperListAdapter(ExamListActivity.this, data,
				listView);
		listView.setAdapter(mAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ExamListActivity.this,
						PaperActivity.class);
				intent.putExtra(HctConstants.ON_ACTIVITY_KEY_PAPER_ENTITY,
						data.get(position - 1));
				startActivity(intent);
			}
		});
		final View loadView = convertView
				.findViewById(R.id.lyt_default_list_load);
		// mAdapter = new VideoDetailAdapter(LearningListActivity.this, data,
		// gridView.getRefreshableView());
		// gridView.getRefreshableView().setAdapter(mAdapter);
		// gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
		//
		// @Override
		// public void onPullDownToRefresh(
		// PullToRefreshBase<GridView> refreshView) {// 下拉刷新
		// pageNum = 1;
		// initData(gridView, loadView, typeId, null, false);
		// }
		//
		// @Override
		// public void onPullUpToRefresh(
		// PullToRefreshBase<GridView> refreshView) {// 上拉加载更多
		// pageNum++;
		// initData(gridView, loadView, typeId, null, false);
		// }
		//
		// });
		//
		initData(loadView, true);

	}

	private void initData(final View loadView, final boolean showLoadView) {
		final Handler handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				switch (msg.what) {

				case HctConstants.STATUS_FAILD:
					if (pageNum == 1) {
						data.clear();
						mAdapter.notifyDataSetChanged();
					}
					break;
				case HctConstants.STATUS_SUCCESS:
					HctResult hctResult = (HctResult) msg.obj;
					if (hctResult != null && hctResult.data != null) {
						List<ExamPaperEntity> netData = (List<ExamPaperEntity>) hctResult.data;
						if (netData != null && netData.size() > 0) {
							if (pageNum > 1) {// 加载更多
								data.addAll(netData);
							} else {// 首页数据
								data.clear();
								data.addAll(netData);
							}
							if (netData.size() < 20) {
								listView.setPullLoadEnable(false);
							}
						} else {
							listView.setPullLoadEnable(false);
						}
						mAdapter.notifyDataSetChanged();
					}
					break;

				default:
					break;
				}
				loadView.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				if (pageNum == 1) {
					listView.stopRefresh();
				} else {
					listView.stopLoadMore();
				}
				super.dispatchMessage(msg);
			}
		};

		if (showLoadView) {
			loadView.setVisibility(View.VISIBLE);
		}
		new Thread() {
			public void run() {
				HctController controller = new HctController(
						ExamListActivity.this);
				try {
					HctResult hctResult = controller.getExamPagesList(moduleId,
							childtypeId, searchKey);
					handler.sendMessage(handler.obtainMessage(
							HctConstants.STATUS_SUCCESS, hctResult));
				} catch (Exception e) {
					handler.sendEmptyMessage(HctConstants.STATUS_FAILD);
					e.printStackTrace();
				}
			};
		}.start();

	}

	/**
	 * 初始化标题栏
	 */
	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.exam_papers_title));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

	@Override
	public void onRefresh() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		listView.setRefreshTime(dateFormat.format(new Date()));
		pageNum = 1;
		initData(getLoadView(), false);
	}

	@Override
	public void onLoadMore() {
		pageNum++;
		initData(getLoadView(), false);
	}

	private View getLoadView() {
		View convertView = findViewById(R.id.lyt_default_content);
		return convertView.findViewById(R.id.lyt_default_list_load);
	}

}
